package com.tf.temper.controller;

import com.ccssoft.m8.common.web.BaseController;
import com.tf.temper.entity.DeviceReturnEntity;
import com.tf.temper.entity.LogeventEntity;
import com.tf.temper.entity.UploadfeedbackresultListEntity;
import com.tf.temper.service.LogeventService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 上传温度
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
@Api(value = "上传温度",description="DevuploadtemperController")
@RestController
@RequestMapping("/syattribute")
public class DevuploadtemperController extends BaseController
{
    @Autowired
    private LogeventService<LogeventEntity> logeventService;
    @ApiOperation("上传温度接口")
    @PostMapping("/{deviceid}")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public DeviceReturnEntity<String> syattribute(@PathVariable("deviceid") String deviceid, @RequestBody UploadfeedbackresultListEntity feedbackentity)
    {
        LogeventEntity templogevent = new LogeventEntity();
        templogevent.setTemperature(feedbackentity.getInfo().getTemperature());
        templogevent.setDeviceid(deviceid);
        templogevent.setUuid(feedbackentity.getInfo().getUuid());
        templogevent.setMask(feedbackentity.getInfo().getMask()==false?1:0);
        templogevent.setNationCode(feedbackentity.getInfo().getNationCode());
        if(feedbackentity.getInfo().getOpenCode()==100)
        {
            templogevent.setOpenCode(1);
        }
        else
        {
            templogevent.setOpenCode(0);
        }
        templogevent.setOpenMsg(feedbackentity.getInfo().getOpenMsg());
        templogevent.setUserType("user");
        templogevent.setScreenTime(feedbackentity.getInfo().getScreenTime());
        try
        {
            BASE64Decoder mdecoder = new BASE64Decoder();
            String format = "yyyy-MM-dd";
            SimpleDateFormat sdf = new  SimpleDateFormat(format);
            String curDate =  sdf.format(new Date());
            String imgpath = curDate+deviceid+System.currentTimeMillis()+".jpg";
            File file = new File("/images/"+imgpath);
            if (!file.exists())
            {
                file.createNewFile();
            }
            byte[] buf = mdecoder.decodeBuffer(feedbackentity.getInfo().getImg());
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(buf);
            fileOutputStream.flush();
            fileOutputStream.close();
            templogevent.setImgurl("http://healthy-frontend-gcgs-cx-gc-20200902.ccsyun.vip/dataimages/"+imgpath);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        logeventService.insert(templogevent);
        return  new DeviceReturnEntity<String>().ok("0");
    }
}